var a;
function SwitchCheckboxModTools(ComID) {
		var frmSplit = document.getElementById('frmPostSplitMod');
		var frmMerge = document.getElementById('frmPostMergeMod');
		comField = 'Comment_' + ComID;
		el = document.getElementById(comField);
    var Item = document.getElementById(ComID);
	var val = Item.value;
	if (val == 0) {
		Item.value = 1; 
		Item.checked = 1;
		el.className = 'selected';
		if(frmSplit) {addToFormModTools('frmPostSplitMod',ComID);}
		if(frmMerge) {addToFormModTools('frmPostMergeMod',ComID);}
		} else {
			Item.value = 0; 
			Item.checked = 0;
			el.className = 'unselected';
			if(frmSplit) {removeFormModTools('frmPostSplitMod',ComID);}
			if(frmMerge) {removeFormModTools('frmPostMergeMod',ComID);}
			}

}

function removeFormModTools (formID,ComID) {
	var myForm = document.getElementById(formID);
	if (myForm.hasChildNodes()) {
		var Input = document.getElementsByTagName('input');
		for (var i=0; i < Input.length; i++) {
			var el = Input[i];
			elVal = el.getAttribute('value');
			if (elVal == ComID) {
				myForm.removeChild(el);
				};
		};
	};
}

function addToFormModTools (formID,ComID) {
	var myForm = document.getElementById(formID);
	if (myForm.hasChildNodes()) {
		var newInput = document.createElement('input');
		newInput.setAttribute('type', 'hidden');
		newInput.setAttribute('value', ComID);
		newInput.setAttribute('name','ComID[]');
		myForm.appendChild(newInput);
	};
}
function checkAll () {
	var Input = document.getElementsByTagName('input');
	unchk = document.getElementById('checkAll');
	if (unchk) {
		a = 0;
		unchk.innerHTML = 'UnCheck All';
		unchk.setAttribute('id', 'uncheckAll');
	} else	{
		a = 1;
		unchk = document.getElementById('uncheckAll');
		unchk.innerHTML = 'Check All For Total Merge'
		unchk.setAttribute('id', 'checkAll');
	}
	for (var i=0; i < Input.length; i++) {
		var el = Input[i];
		eltype = el.getAttribute('type');
		ComID = el.getAttribute('id');
		elVal = el.getAttribute('value');
		if (eltype == 'checkbox' && elVal == a) {
			SwitchCheckboxModTools(ComID);
			if (a==1){i = i - 1;}
		};
		
	};
	
	//TODO uncheck all function here...
}

function selectCategory (catIDnum,DiscussionID) {
	var el = document.getElementById('moveDiscussion');
	var catID = String(catIDnum);
	el.setAttribute('href','./extension.php?PostBackAction=MoveIt&DiscussionID=' + DiscussionID + '&CategoryID=' + catID)
}

function _setTitle (request) {
	if(request.responseText != '')
	{
		title =  prompt("Edit Title and then click on 'Set New Title Now!'",request.responseText);
		var el = document.getElementById('subjectTitle');
		oldAction = el.getAttribute('href');
		el.setAttribute('href', oldAction + title);
	}
	else
	{
		alert('Error: Empty string '+ request.responseText );
	}
}

function urlDiscID (DiscussionID) {
	var el = document.getElementById('subjectTitle');
	el.setAttribute('href','./extension.php?PostBackAction=EditSubject&DiscussionID=' + DiscussionID + '&Subject=');
}

function setTitle(baseurl, DiscussionID)
{
	var dm = new DataManager();	
	dm.RequestCompleteEvent = _setTitle;
	dm.RequestFailedEvent = _setTitle;
	dm.LoadData(baseurl + 'extensions/ModTools/ajax.php?DiscussionID=' + DiscussionID);
}

